using System;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.Model.ScriptDirectives
{
    public class OrderDirective : IScriptDirective
    {
        private readonly string executionOrder;

        public OrderDirective([NotNull] string executionOrder)
        {
            if (executionOrder == null) throw new ArgumentNullException("executionOrder");

            var orderType = executionOrder.ToLowerInvariant();

            if (orderType != "round-robin" && orderType != "random")
                throw new ArgumentOutOfRangeException("executionOrder", executionOrder, "Expected value to be \"round-robin\" or \"random\"");

            this.executionOrder = orderType;
        }

        public CompiledNode Apply([NotNull] CompiledScriptModule module)
        {
            if (module == null) throw new ArgumentNullException("module");

            module.ExecutionOrder = executionOrder == "round-robin" ? (OperationOrder) new RoundRobinOperationOrder() : new RandomOperationOrder();
            return new CompiledNode(module);
        }
    }
}